package org.jeecg.modules.api.controller.music.login;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.api.vo.user.UserDetailVo;
import org.jeecg.modules.music.entity.MusUser;
import org.jeecg.modules.music.service.IMusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(tags="音乐系统")
@RestController
@RequestMapping("api/mus/")
public class LoginMusController {

    @Autowired
    private IMusUserService musUserService;

    @Autowired
    private RedisUtil redisUtil;
    @ApiOperation("API-登陆-邮箱登录（密码）")
    @RequestMapping(value = "login/cellemail", method = RequestMethod.POST)
    public Result reqCellEmailByPassword(@RequestParam(name = "email",required = true) String email, @RequestParam(name = "password",required = true) String password){

        Map<String,Object> map = new HashMap<>();
        MusUser user = musUserService.getUserByEmail(email);
        if (user==null){
            return Result.error(401,"邮箱未注册");
        }else {
            if (user.getPassword().equals(password)){
                user.setPassword(null);
                map.put("cookie", UUIDGenerator.generate());
                map.put("account",user);
                return Result.ok(map);
            }else {
                return Result.error(402,"密码输入不正确");
            }
        }
    }

    @ApiOperation("API-退出")
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public Result<JSONObject> logout(){
        Result<JSONObject> result = new Result<JSONObject>();
        result.setMessage("成功退出");
        result.setSuccess(true);
        return result;
    }

    /**
     *  校验用户账号是否唯一<br>
     *  可以校验其他 需要检验什么就传什么。。。
     *
     * @param sysUser
     * @return
     */
    @ApiOperation("API-校验用户账号是否存在")
    @RequestMapping(value = "/user/checkExistUser", method = RequestMethod.GET)
    public Result<Boolean> checkExistUser(MusUser sysUser) {
        Result<Boolean> result = new Result<>();
        //如果此参数为false则程序发生异常
        result.setResult(true);
        try {
            //通过传入信息查询新的用户信息
            sysUser.setPassword(null);
            MusUser user = musUserService.getOne(new QueryWrapper<MusUser>(sysUser));
            if (user == null) {
                result.setSuccess(false);
                result.setMessage("邮箱地址或手机号码未注册！");
                return result;
            }

        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage(e.getMessage());
            return result;
        }
        result.setSuccess(true);
        return result;
    }

   /**
     * 手机号短信登录接口
     *
     * @return
     */
    @ApiOperation("API-登陆-手机号短信登录接口")
    @RequestMapping(value = "/login/cellphone/sms", method = RequestMethod.POST)
    public Result phoneLoginSms(@RequestParam(name = "phone",required = true) String phone,@RequestParam(name = "captcha",required = true) String smscode) {
        Map<String,Object> map = new HashMap<>();
        if (oConvertUtils.isEmpty(phone)||phone.equals(" ")||phone.equals("undefined")){
            return Result.error(401,"请输入手机号码！");
        }else {
            MusUser user = musUserService.getUserByPhone(phone);
            UserDetailVo profile = musUserService.getUserProfileByPhone(phone);
            if (user==null){
                return Result.error(402,"手机号码未注册！");
            }else {
                Object code = redisUtil.get(phone);
                if (!smscode.equals(code)) {
                    return Result.error(403,"手机验证码错误！");
                }
                user.setPassword(null);
                map.put("cookie", UUIDGenerator.generate());
                map.put("account",user);
                map.put("profile",profile);
                return Result.ok(map);
            }
        }
    }

    /**
     * 手机号密码登录接口
     *
     * @return
     */
    @ApiOperation("API-登陆-手机号密码登录接口")
    @RequestMapping(value = "/login/cellphone/pass", method = RequestMethod.POST)
    public Result phoneLoginPass(@RequestParam(name = "phone",required = true) String phone,@RequestParam(name = "password",required = true) String password){
        Map<String,Object> map = new HashMap<>();
        if (oConvertUtils.isEmpty(password)&&oConvertUtils.isEmpty(phone)){
            return Result.error(401,"请输入手机号码和密码！");
        }else {
            MusUser user = musUserService.getUserByPhone(phone);
            UserDetailVo profile = musUserService.getUserProfileByPhone(phone);
            if (user==null){
                return Result.error(402,"手机号码未注册！");
            }else {
                String pass = user.getPassword();
                if (!password.equals(pass)) {
                    return Result.error(403,"密码错误！！");
                }
                user.setPassword(null);
                map.put("cookie", UUIDGenerator.generate());
                map.put("account",user);
                map.put("profile",profile);
                return Result.ok(map);
            }
        }
    }
}
